﻿using B.S.XZYData.Infrastructure.Interface;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace B.S.XZYData.Infrastructure.Inpement
{
    /// <summary>
    /// 基础操作实现层
    /// </summary>
    public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
    {
        private readonly MyDbContext db;

        public BaseRepository(MyDbContext db)
        {
            this.db = db;
        }
        
        /// <summary>
        /// 根据主键查询
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>返回model实体</returns>
        public async Task<T> GetValue(long id)
        {
            return await db.Queryable<T>().InSingleAsync(id);
        }
        
        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns>返回所有数据</returns>
        public ISugarQueryable<T> GetAll()
        {
            return db.Queryable<T>();
        }

        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="expression">查询条件</param>
        /// <returns>返回查询结果</returns>
        public async Task<List<T>> GetAllAsync(Expression<Func<T, bool>> expression)
        {
            return await db.Queryable<T>().Where(expression).ToListAsync();
        }
        
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="t">实体</param>
        /// <returns>返回成功或失败</returns>
        public async Task<int> CreateAsync(T t)
        {
            // 使用ExecuteCommandAsync替代ExecuteReturnIdentityAsync，返回受影响的行数
            // 这样可以避免在某些数据库配置下无法获取自增ID的问题
            return await db.Insertable(t).ExecuteCommandAsync();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t">实体</param>
        /// <returns>返回成功或失败</returns>
        public async Task<int> UpdateAsync(T t)
        {
            return await db.Updateable(t).ExecuteCommandAsync();
        }
        
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>返回成功或失败</returns>
        public async Task<int> DeleteAsync(long id)
        {
            return await db.Deleteable<T>().In(id).ExecuteCommandAsync();
        }
        
        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="entities">实体列表</param>
        /// <returns>返回成功或失败</returns>
        public async Task<int> CreateBatchAsync(List<T> entities)
        {
            return await db.Insertable(entities).ExecuteCommandAsync();
        }
        
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="entities">实体列表</param>
        /// <returns>返回成功或失败</returns>
        public async Task<int> UpdateBatchAsync(List<T> entities)
        {
            return await db.Updateable(entities).ExecuteCommandAsync();
        }
        
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="entities">实体列表</param>
        /// <returns>返回成功或失败</returns>
        public async Task<int> DeleteBatchAsync(List<T> entities)
        {
            return await db.Deleteable(entities).ExecuteCommandAsync();
        }
        
        /// <summary>
        /// 开始事务
        /// </summary>
        /// <returns></returns>
        public async Task BeginTranAsync()
        {
            await db.Ado.BeginTranAsync();
        }
        
        /// <summary>
        /// 提交事务
        /// </summary>
        /// <returns></returns>
        public async Task CommitTranAsync()
        {
            await db.Ado.CommitTranAsync();
        }
        
        /// <summary>
        /// 回滚事务
        /// </summary>
        /// <returns></returns>
        public async Task RollbackTranAsync()
        {
            await db.Ado.RollbackTranAsync();
        }
    }
}
